package org.htmlparser.lexer;

import java.io.Serializable;
import org.htmlparser.util.sort.Ordered;
import org.htmlparser.util.sort.Sort;
import org.htmlparser.util.sort.Sortable;

public class PageIndex
  implements Serializable, Sortable
{
  protected static final int mStartIncrement = 100;
  protected int mCount;
  protected int mIncrement;
  protected int[] mIndices;
  protected Page mPage;

  public PageIndex(Page paramPage)
  {
    this.mPage = paramPage;
    this.mIndices = new int[this.mIncrement];
    this.mCount = 0;
    this.mIncrement = 200;
  }

  public PageIndex(Page paramPage, int paramInt)
  {
    this(paramPage);
    this.mIndices[0] = paramInt;
    this.mCount = 1;
  }

  public PageIndex(Page paramPage, int[] paramArrayOfInt)
  {
    this.mPage = paramPage;
    this.mIndices = paramArrayOfInt;
    this.mCount = paramArrayOfInt.length;
  }

  public int add(int paramInt)
  {
    return add(new Cursor(getPage(), paramInt));
  }

  public int add(Cursor paramCursor)
  {
    int k;
    int i = paramCursor.getPosition();
    if (this.mCount == 0)
    {
      k = 0;
      insertElementAt(i, 0);
    }
    while (true)
    {
      do
      {
        while (true)
        {
          int j;
          while (true)
          {
            return k;
            j = this.mIndices[(this.mCount - 1)];
            if (i != j)
              break;
            k = this.mCount - 1;
          }
          if (i <= j)
            break;
          k = this.mCount;
          insertElementAt(i, k);
        }
        k = Sort.bsearch(this, paramCursor);
      }
      while ((k < size()) && (i == this.mIndices[k]));
      insertElementAt(i, k);
    }
  }

  protected int bsearch(int paramInt)
  {
    return Sort.bsearch(this, new Cursor(getPage(), paramInt));
  }

  protected int bsearch(int paramInt1, int paramInt2, int paramInt3)
  {
    return Sort.bsearch(this, new Cursor(getPage(), paramInt1), paramInt2, paramInt3);
  }

  public int capacity()
  {
    return this.mIndices.length;
  }

  public int column(int paramInt)
  {
    return column(new Cursor(getPage(), paramInt));
  }

  public int column(Cursor paramCursor)
  {
    int j;
    int i = row(paramCursor);
    if (i != 0)
      j = elementAt(i - 1);
    while (true)
    {
      return (paramCursor.getPosition() - j);
      j = 0;
    }
  }

  public int elementAt(int paramInt)
  {
    if (paramInt >= this.mCount)
      throw new IndexOutOfBoundsException("index " + paramInt + " beyond current limit");
    return this.mIndices[paramInt];
  }

  public Ordered fetch(int paramInt, Ordered paramOrdered)
  {
    Cursor localCursor;
    if (paramOrdered != null)
    {
      localCursor = (Cursor)paramOrdered;
      localCursor.mPosition = this.mIndices[paramInt];
      localCursor.mPage = getPage();
    }
    while (true)
    {
      return localCursor;
      localCursor = new Cursor(getPage(), this.mIndices[paramInt]);
    }
  }

  public int first()
  {
    return 0;
  }

  public int[] get()
  {
    int[] arrayOfInt = new int[size()];
    System.arraycopy(this.mIndices, 0, arrayOfInt, 0, size());
    return arrayOfInt;
  }

  public Page getPage()
  {
    return this.mPage;
  }

  protected void insertElementAt(int paramInt1, int paramInt2)
  {
    int[] arrayOfInt;
    if ((paramInt2 >= capacity()) || (size() == capacity()))
    {
      arrayOfInt = new int[Math.max(capacity() + this.mIncrement, paramInt2 + 1)];
      this.mIncrement = (2 * this.mIncrement);
      if (paramInt2 < capacity())
      {
        System.arraycopy(this.mIndices, 0, arrayOfInt, 0, paramInt2);
        System.arraycopy(this.mIndices, paramInt2, arrayOfInt, paramInt2 + 1, capacity() - paramInt2);
        this.mIndices = arrayOfInt;
      }
    }
    while (true)
    {
      do
        while (true)
        {
          this.mIndices[paramInt2] = paramInt1;
          this.mCount = (1 + this.mCount);
          return;
          System.arraycopy(this.mIndices, 0, arrayOfInt, 0, capacity());
        }
      while (paramInt2 >= size());
      System.arraycopy(this.mIndices, paramInt2, this.mIndices, paramInt2 + 1, capacity() - paramInt2 + 1);
    }
  }

  public int last()
  {
    return (this.mCount - 1);
  }

  public void remove(int paramInt)
  {
    remove(new Cursor(getPage(), paramInt));
  }

  public void remove(Cursor paramCursor)
  {
    int i = Sort.bsearch(this, paramCursor);
    if ((i < size()) && (paramCursor.getPosition() == this.mIndices[i]))
      removeElementAt(i);
  }

  protected void removeElementAt(int paramInt)
  {
    System.arraycopy(this.mIndices, paramInt + 1, this.mIndices, paramInt, capacity() - paramInt + 1);
    this.mIndices[(capacity() - 1)] = 0;
    this.mCount -= 1;
  }

  public int row(int paramInt)
  {
    return row(new Cursor(getPage(), paramInt));
  }

  public int row(Cursor paramCursor)
  {
    int i = Sort.bsearch(this, paramCursor);
    if ((i < this.mCount) && (paramCursor.getPosition() == this.mIndices[i]))
      ++i;
    return i;
  }

  public int size()
  {
    return this.mCount;
  }

  public void swap(int paramInt1, int paramInt2)
  {
    int i = this.mIndices[paramInt1];
    this.mIndices[paramInt1] = this.mIndices[paramInt2];
    this.mIndices[paramInt2] = i;
  }
}